﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Text.RegularExpressions;

namespace WebpiketTable
{
	/// <summary>
	/// Логика взаимодействия для MainWindow.xaml
	/// </summary>
	public partial class MainWindow : Window
	{
		public MainWindow()
		{
			InitializeComponent();
			PrjProperty.Init();
		}

		private void Button_Click_1(object sender, RoutedEventArgs e)
		{
			// Create OpenFileDialog
			Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();

			// Set filter for file extension and default file extension
			dlg.DefaultExt = ".dat";
			dlg.Filter = "Compass file (.dat)|*.dat";

			// Display OpenFileDialog by calling ShowDialog method
			Nullable<bool> result = dlg.ShowDialog();

			// Get the selected file name and display in a TextBox
			if (result == true)
			{
				#region ReadFile
				// Open document
				string filename = dlg.FileName;
				// Open the file into a StreamReader
				StreamReader file = File.OpenText(filename);
				// Read the file into a string
				string sdata = file.ReadToEnd();
				file.Close();
				string regtemp = @"\s+\w+" //From
					+ @"\s+\w+" //To
					+ @"\s+\d+(\.\d+)?" //Length
					+ @"\s+\d+(\.\d+)?" //Azimuth
					+ @"\s+(\-)?\d+(\.\d+)?"//Inc
					+ @"\s+(\-)?\d+(\.\d+)?" //L
					+ @"\s+(\-)?\d+(\.\d+)?" //U
					+ @"\s+(\-)?\d+(\.\d+)?" //D
					+ @"\s+(\-)?\d+(\.\d+)?" //R
					+ @"(\s+\w+)*" //Comments
					+ @"(\s+)*" //Comments
					+ @"\r"; //EndOfLine

				Regex RE = new Regex(regtemp, RegexOptions.Multiline);
				MatchCollection theMatches = RE.Matches(sdata);
				foreach (Match match in theMatches)
				{
					string sline = match.ToString();
					sline = Regex.Replace(sline, @"(\s)+", " ");
					Regex regLine = new Regex(@"\s(\-)?\w+(\,\w+)?", RegexOptions.Multiline);
					sline = Regex.Replace(sline, @"\.", ",");
					MatchCollection matchLine = regLine.Matches(sline);
					Piket pkt = new Piket()
					{
						From = matchLine[0].ToString().Trim(),
						To = matchLine[1].ToString().Trim(),
						Length = Convert.ToDouble(matchLine[2].ToString().Trim()) * 0.3048,
						Azimuth = Convert.ToDouble(matchLine[3].ToString().Trim()),
						Inc = Convert.ToDouble(matchLine[4].ToString().Trim()),
						Left = Convert.ToDouble(matchLine[5].ToString().Trim()) * 0.3048,
						Up = Convert.ToDouble(matchLine[6].ToString().Trim()) * 0.3048,
						Down = Convert.ToDouble(matchLine[7].ToString().Trim()) * 0.3048,
						Right = Convert.ToDouble(matchLine[8].ToString().Trim()) * 0.3048
					};
					if (pkt.Left < 0) pkt.Left = 0;
					if (pkt.Right < 0) pkt.Right = 0;
					if (pkt.Up < 0) pkt.Up = 0;
					if (pkt.Down < 0) pkt.Down = 0;
					/*pkt.From = matchLine[0].ToString().Trim();
					pkt.To = matchLine[1].ToString().Trim();*/
					PrjProperty.pikets.Add(pkt);
				} 
				#endregion
			}

			dgTable.ItemsSource = null;
			dgTable.ItemsSource = PrjProperty.pikets;
		}

		private void Button_Click_2(object sender, RoutedEventArgs e)
		{
			foreach (Piket pkt in PrjProperty.pikets)
			{
				pkt.dx = pkt.Length * Math.Cos((pkt.Azimuth - 90) * Math.PI / 180) * Math.Cos((pkt.Inc) * Math.PI / 180);
				pkt.dy = -pkt.Length * Math.Sin((pkt.Azimuth - 90) * Math.PI / 180) * Math.Cos((pkt.Inc) * Math.PI / 180);
				pkt.dz = pkt.Length * Math.Sin((pkt.Inc) * Math.PI / 180);
			}
			foreach (Piket pkt in PrjProperty.pikets)
			{
				foreach (Piket pktFind in PrjProperty.pikets)
				{
					if (pkt.From == pktFind.To)
					{
						pkt.x = pktFind.x + pktFind.dx;
						pkt.y = pktFind.y + pktFind.dy;
						pkt.z = pktFind.z + pktFind.dz;
					}
				}
			}
			foreach (Piket pkt in PrjProperty.pikets)
			{
				foreach (Piket pktFind in PrjProperty.pikets)
				{
					if (pkt.To == pktFind.From)
					{
						pkt.Left2 = pktFind.Left;
						pkt.Right2 = pktFind.Right;
						pkt.Up2 = pktFind.Up;
						pkt.Down2 = pktFind.Down;
					}
				}
			}
			dgTable.ItemsSource = null;
			dgTable.ItemsSource = PrjProperty.pikets;
		}

		private void Button_Click_3(object sender, RoutedEventArgs e)
		{
			string str = "";
			string strSurvey = "";
			foreach (Piket pkt in PrjProperty.pikets)
			{
				// setpiket px py pz plen  pazim  pug  pleft1 pright1 pup1 pdown1  pleft2 pright2 pup2 pdown2
				str += string.Format("setpiket {0} {1} {2} ", pkt.x, pkt.y, pkt.z);
				str += string.Format("{0} {1} {2} ", pkt.Length, pkt.Azimuth, pkt.Inc);
				str += string.Format("{0} {1} {2} {3} ", pkt.Left, pkt.Right, pkt.Up, pkt.Down);
				str += string.Format("{0} {1} {2} {3} \n", pkt.Left2, pkt.Right2, pkt.Up2, pkt.Down2);
				strSurvey += string.Format("setpiket {0} {1} {2} ", pkt.x, pkt.y, pkt.z);
				strSurvey += string.Format("{0} {1} {2} ", pkt.Length, pkt.Azimuth, pkt.Inc);
				strSurvey += string.Format("0.2 0.2 0.2 0.2 ");
				strSurvey += string.Format("0.2 0.2 0.2 0.2 \n");
			}
			str = Regex.Replace(str, ",", ".");
			strSurvey = Regex.Replace(strSurvey, ",", ".");
			#region fn setpiket
			string strfn = @"
fn setpiket px py pz plen  pazim  pug  pleft1 pright1 pup1 pdown1  pleft2 pright2 pup2 pdown2 =
(

  pradius1 = pdown1 + pup1;
  if (pleft1 + pright1 > pdown1 + pup1) then pradius1 = pleft1 + pright1;
  pradius2 = pdown2 + pup2;
  if (pleft2 + pright2 > pdown2 + pup2) then pradius2 = pleft2 + pright2;
  pradius = pradius2;
  if (pradius1 > pradius2) then pradius = pradius1;

  pradius = pradius / 2.0;

  cyl = cylinder Radius:pradius Height:plen HeightSegs:2 Sides:8;

  cyl.rotation.z_rotation = 180 - pazim;
  cyl.rotation.x_rotation = 90-pug;
  cyl.pos = [px,py,pz]

  addModifier cyl (ffd_2x2x2());
  animateAll cyl.ffd_2x2x2;

  ad1 = (sqrt(2) * pdown1) / (2 * pradius);
  au1 = (sqrt(2) *  pup1) / (2 * pradius);
  al1 = (sqrt(2) *  pleft1) / (2 * pradius);
  ar1 = (sqrt(2) *  pright1) / (2 * pradius);

  ad2 = (sqrt(2) * pdown2) / (2 * pradius);
  au2 = (sqrt(2) *  pup2) / (2 * pradius);
  al2 = (sqrt(2) *  pleft2) / (2 * pradius);
  ar2 = (sqrt(2) *  pright2)  / (2 * pradius);

  --down
  cyl.ffd_2x2x2.control_point_1 = [0.5, 0.5 - ad1, 0];
  cyl.ffd_2x2x2.control_point_2 = [0.5, 0.5 - ad2, 1];
  --up  
  cyl.ffd_2x2x2.control_point_7 = [0.5, 0.5 + au1, 0];
  cyl.ffd_2x2x2.control_point_8 = [0.5, 0.5 + au2, 1];
  --right
  cyl.ffd_2x2x2.control_point_3 = [0.5 - ar1, 0.5 , 0];
  cyl.ffd_2x2x2.control_point_4 = [0.5 - ar2, 0.5 , 1];
  --left    
  cyl.ffd_2x2x2.control_point_5 = [0.5 + al1, 0.5 , 0];
  cyl.ffd_2x2x2.control_point_6 = [0.5 + al2, 0.5 , 1];

  addModifier cyl (edit_mesh());
)

";
			#endregion
			using (StreamWriter sw = new StreamWriter(@"C:\!project\Webpiket3\misc\GenPiketFile.ms"))
			{
				sw.WriteLine("-- Gen by Webpiket ");
				sw.WriteLine("-- Gen date is: " + DateTime.Now);
				sw.Write(strfn);
				sw.Write("\n\r");
				sw.Write(str);
			}
			using (StreamWriter sw = new StreamWriter(@"C:\!project\Webpiket3\misc\GenPiketFile-survey.ms"))
			{
				sw.WriteLine("-- Gen by Webpiket ");
				sw.WriteLine("-- Gen date is: " + DateTime.Now);
				sw.Write(strfn);
				sw.Write("\n\r");
				sw.Write(strSurvey);
			}
		}
	}
}
